import json

from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect, JsonResponse
from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse

def login(request):
    return HttpResponse('这是用户登录页面')

def user_center(request):
    # 如果用户没有登录, 就重定向到 登录页面, 登录页面的url地址:  login/
    # url(r'^mylogin/$', views.login, name='login'),
    url= reverse('app1:login')  # reverse 中写 路由的名字  name=  的值
    return redirect(url)

def axios_get(request):
    name = request.GET.get('name')
    age = request.GET.get('age')

    # return JsonResponse({'name':name, 'age':age})

def axios_post(request):
    name = request.POST.get('name')
    age = request.POST.get('age')
    print(request.POST)
    print(name)
    print(age)

    body_data = request.body.decode()
    dict_data = json.loads(body_data)
    print(body_data)
    name2 = dict_data.get('name')
    age2 = dict_data.get('age')
    print(name2)
    print(age2)

    return JsonResponse({'name':name2, 'age':age2})

def index_git(request):
    return HttpResponse('很开心,今天用git 管理了项目')

# def index(request):
#     # login
#     data = request.GET  # data -> 查询字典
#     print(data)
#     print(data.get('name'))
#     print(data.getlist('name'))
#     return HttpResponse('app1 login')
#
#
# def index123(request):
#     # user_center
#     myurl = reverse('app1:login')  # login/
#     # return redirect('/login/')
#     return redirect(myurl)
#     # return HttpResponse('user_center')
#
#
# def get(request):
#     # login
#     # ?name=zhangsan&name=lisi
#     data = request.GET  # data -> 查询字典  {'name':['zhangsan','lisi']}
#     print(data)
#     print(data.get('name'))
#     print(data.getlist('name'))
#     return HttpResponse('get')
#
#
# def post(request):
#     # 保单提交  Content-Type = application/x-www-form-urlencoded
#     # print(request.META)
#     # data = request.POST  # data -> 查询字典
#     # data_body = request.body  # data -> 查询字典
#     # print(data)
#     # print(data_body)
#     # print(data.get('name'))
#     # print(data.getlist('name'))
#     # return HttpResponse('post')
#     # 非表单提交
#     print(request.META['CONTENT_TYPE'])
#     data_post = request.POST  # data -> 查询字典
#     data_body = request.body  # data -> 查询字典
#     print(data_post)
#     print(data_body)  # b'{"name":"zhangsan"}'
#     dict_body = json.loads(data_body)
#     print(dict_body.get('name'))
#     return HttpResponse('post')
#
#
# def put(request):
#     data = request.body  # byte 类型的字符串
#     dict_data = json.loads(data)  # json 字符串 转换成 字典
#     print(dict_data.get('name'))
#     return HttpResponse('put')
#
#
# def delete(request):
#     data = request.body  # byte 类型的字符串
#     dict_data = json.loads(data)  # json 字符串 转换成 字典
#     print(dict_data.get('name'))
#     return HttpResponse('delete')
#
#
# def urlPath(request, year, city):
#     print(city)
#     print(year)
#     return HttpResponse("城市:%s, 年份:%s" % (city, year))
#
#
# def header(request):
#     # name ->  HTTP_NAME
#     # age -> HTTP_AGE
#     print(type(request.META))
#     print(request.META.get('HTTP_NAME'))
#     return HttpResponse("header")
#
#
# def other(request):
#     print(request.method)  # 请求方法
#     # print(request.user)  # 用户对象
#     print(request.path)  # 请求路径
#     print(request.encoding)  # 表示提交的数据的编码方式
#     print(request.FILES)  # 如果是通过post 提交文件
#
#     return HttpResponse('other')
#
#
# def myresponse(request):
#     """
#     http 响应
#     code
#     content_type
#     """
#     # response = HttpResponse(content='myresponse', content_type='json', status=300)
#     # # 给响应头加数据, 把response 当做字典, 直接添加键值对就可以.
#     # response['name'] = 'zhangsan'
#
#     response = HttpResponse()
#     response.content = '<h1>400 bad request</h1>'
#     response.status_code = 400
#     # response.content_type
#     response['Content-Type'] = 'text/html'
#
#     return response
#
# def not_found(request):
#     return HttpResponseNotFound()
#     # return HttpResponse(status=404)
#
# def myresponse301(request):
#     return HttpResponseRedirect('/login/')
#     # return HttpResponse(status=404)
#
#
# from django.http import JsonResponse
#
# def json_view(request):
#     return JsonResponse({'city': 'beijing', 'subject': 'python'})
#
# def my_json_view(request):
#     d = {'city': 'beijing', 'subject': 'python'}
#     json_str = json.dumps(d)
#     response = HttpResponse(content=json_str, content_type='application/json')
#     return response
#
# def my_redirect(request):
#     return redirect('/my_login/')
#
#
# def my_redirect2(request):
#     response = HttpResponse('', status=302)
#     response['Location'] = '/my_login/'
#     return response
#
# def my_post(request):
#     # 1.接收表单请求
#     body_data = request.body.decode()
#     # post 方式 传表单数据, 原始数据存在于 body中.
#     # 格式 b'k=v&k1=v1&k2=v2'
#     post_data = request.POST
#     # post_data  是  body中的数据, 转换成的字典,
#     # 是一个querydict 对象,  类似于 dict, 可以使用get方法取键对应的值, getlist方法, 取键对应的多个值
#     print(body_data)
#     print(post_data)
#     print(post_data.get('name'))
#     print(post_data.getlist('name'))
#
#     return HttpResponse('post 表单数据')
#
# def my_post2(request):
#     #1. 接收非标单数据
#     body_data = request.body.decode()   # post 的原始数据一定在body中有一份
#     post_data = request.POST # 此时,POST中并没有数据, 因为不是表单格式,
#     print(body_data) # 字符串
#     print(type(body_data))  # str
#     print(post_data)  # 空
#
#     body_dict = json.loads(body_data)  # 把body转成字典了
#     print(body_dict.get('name'))
#
#     return HttpResponse('post 非标单数据')






# class JsonResponse(HttpResponse):
#     """
#     An HTTP response class that consumes data to be serialized to JSON.
#
#     :param data: Data to be dumped into json. By default only ``dict`` objects
#       are allowed to be passed due to a security flaw before EcmaScript 5. See
#       the ``safe`` parameter for more information.
#     :param encoder: Should be an json encoder class. Defaults to
#       ``django.core.serializers.json.DjangoJSONEncoder``.
#     :param safe: Controls if only ``dict`` objects may be serialized. Defaults
#       to ``True``.
#     :param json_dumps_params: A dictionary of kwargs passed to json.dumps().
#     """
#
#     def __init__(self, data, encoder='', safe=True,
#                  json_dumps_params=None, **kwargs):
#         if safe and not isinstance(data, dict):
#             raise TypeError(
#                 'In order to allow non-dict objects to be serialized set the '
#                 'safe parameter to False.'
#             )
#         if json_dumps_params is None:
#             json_dumps_params = {}
#         kwargs.setdefault('content_type', 'application/json')
#         data = json.dumps(data, cls=encoder, **json_dumps_params)  # dict ->  json_str
#         super(JsonResponse, self).__init__(content=data, **kwargs)